package org.example.base.common.controller;

import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.example.base.common.model.TNoticeModel;
import org.example.base.common.service.TNoticeService;
import org.example.huadianguanli.model.TStorageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * Date: 2024/2/15 13:15
 */
@RestController
@RequestMapping("/notice")
public class TNoticeController extends BaseController<String, TNoticeService, TNoticeModel> {

    @Autowired
    private TNoticeService tNoticeService;

    /**
     * 查询最新的公告
     * @return
     */
    @GetMapping("/getNewNotice")
    public ApiResult getNewNotice(){
        TNoticeModel tNoticeModel = tNoticeService.searchOne(Where.build().limit(1, 1));
        return ApiResult.success(tNoticeModel);
    }

    @Override
    public ApiResult searchPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false)  Map<String, Object> params) {
        SearchPageResult<TNoticeModel> result = new SearchPageResult<>();
        Object title = params.get("title");
        Where where = Where.build().field("1").equal(1);
        if (!ObjectUtils.isEmpty(title)){
            where.and().field("title").like(title);
        }
        int totalCount = tNoticeService.count(where);
        List<TNoticeModel> resultList = tNoticeService.search(where.limit(page, limit));
        result.setList(resultList);
        result.setTotalCount(totalCount);
        result.setPage(page);
        result.setLimit(limit);
        return ApiResult.success(result);
    }
}
